import {
    Comment
} from './comment.model'
const comment = new Comment()
Page({
    data: {
        commentList: '',
        page: 1
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {
        let that = this
        that._loadData()
    },

    /**
     * 获取所有数据
     */
    _loadData() {
        let that = this
        // 是否授权
        wx.getSetting({
            success: (res) => {
                if (res.authSetting['scope.userInfo']) {
                    that.getCommentData()
                }
            }
        })
    },

    /**
     * 获取评论列表
     */
    getCommentData() {
        // wx.showLoading({
        //     title: '正在加载数据',
        // })
        let that = this
        comment.getCommentData().then((res) => {
            that.setData({
                commentList: res.data,
            })
        }).then(() => {
            wx.stopPullDownRefresh()
            wx.hideNavigationBarLoading()
            wx.hideLoading()
        })
    },

    /**
     * 新页面中全屏预览图片
     * @param {*} options
     */
    onPreviewImage(options) {
        let that = this,
            commentList = that.data.commentList,
            index = comment.getDataSet(options, 'index')
        wx.previewImage({
            urls: commentList[index]['images']
        })
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        wx.showLoading({
            title: '正在加载数据',
        })
        let that = this
        wx.showNavigationBarLoading()
        that._loadData()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        wx.showLoading({
            title: '正在加载数据',
        })
        let that = this,
            param = {
                page: that.data.page + 1
            }
        that.setData(param)
        comment.getCommentData(param).then((res) => {
            let result = that.data.commentList
            result = result.concat(res.data)
            that.setData({
                commentList: result
            })
        }).then(() => {
            wx.hideLoading()
        })
    }
})